

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>TensorFlow Installation &mdash; 简单粗暴TensorFlow 0.3 beta 文档</title>
  

  
  
  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  

  

  
        <link rel="index" title="索引"
              href="../genindex.html"/>
        <link rel="search" title="搜索" href="../search.html"/>
    <link rel="top" title="简单粗暴TensorFlow 0.3 beta 文档" href="../index.html"/>
        <link rel="next" title="TensorFlow Basic" href="basic.html"/>
        <link rel="prev" title="Preface" href="preface.html"/> 

  
  <script src="../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../index.html" class="icon icon-home"> 简单粗暴TensorFlow
          

          
          </a>

          
            
            
              <div class="version">
                0.3
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../zh/preface.html">前言</a></li>
<li class="toctree-l1"><a class="reference internal" href="../zh/installation.html">TensorFlow安装</a></li>
<li class="toctree-l1"><a class="reference internal" href="../zh/basic.html">TensorFlow基础</a></li>
<li class="toctree-l1"><a class="reference internal" href="../zh/models.html">TensorFlow模型</a></li>
<li class="toctree-l1"><a class="reference internal" href="../zh/extended.html">TensorFlow扩展</a></li>
<li class="toctree-l1"><a class="reference internal" href="../zh/static.html">附录：静态的TensorFlow</a></li>
</ul>
<p class="caption"><span class="caption-text">Contents</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="preface.html">Preface</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">TensorFlow Installation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#simple-installation">Simple Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="#full-installation">Full Installation</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#environment-configuration-before-installation">Environment configuration before installation</a></li>
<li class="toctree-l3"><a class="reference internal" href="#install">Install</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#first-program">First Program</a></li>
<li class="toctree-l2"><a class="reference internal" href="#upgrade-to-a-new-version">Upgrade to A New Version</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="basic.html">TensorFlow Basic</a></li>
<li class="toctree-l1"><a class="reference internal" href="models.html">TensorFlow Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="extended.html">TensorFlow Extensions</a></li>
<li class="toctree-l1"><a class="reference internal" href="static.html">Appendix: Static TensorFlow</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">简单粗暴TensorFlow</a>
        
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> &raquo;</li>
        
      <li>TensorFlow Installation</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
            <a href="../_sources/en/installation.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="tensorflow-installation">
<h1>TensorFlow Installation<a class="headerlink" href="#tensorflow-installation" title="永久链接至标题">¶</a></h1>
<p>The most up-to-date installation method can be acquired through the official website (<a class="reference external" href="https://tensorflow.google.cn/install">https://tensorflow.google.cn/install</a>). TensorFlow supports multiple programming languages like Python, Java and Go and a variety of operating systems like Windows, OSX and Linux. We prefer Python in this handbook.</p>
<p>We provide both simple and full installation methods for readers with different requirements.</p>
<div class="section" id="simple-installation">
<h2>Simple Installation<a class="headerlink" href="#simple-installation" title="永久链接至标题">¶</a></h2>
<p>You can follow these steps if you only want to install TensorFlow on your personal computer without GPU or you do not want to spend too much effort configuring the environment:</p>
<ul class="simple">
<li>Download and install Python distribution <a class="reference external" href="https://www.anaconda.com/download/">Anaconda</a> (with Python Ver 3.6).</li>
<li>Download and install Python IDE <a class="reference external" href="http://www.jetbrains.com/pycharm/">PyCharm</a> (Community version. Students can apply for <a class="reference external" href="https://www.jetbrains.com/student/">licenses of Professional version for free</a>).</li>
<li>Run <code class="docutils literal"><span class="pre">Anaconda</span> <span class="pre">Prompt</span></code> in the Start Menu (Windows), enter and execute <code class="docutils literal"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">tensorflow</span></code>.</li>
<li>Start PyCharm, create a project with default python interpreter, and create a python file in the project.</li>
</ul>
<p>And done.</p>
</div>
<div class="section" id="full-installation">
<h2>Full Installation<a class="headerlink" href="#full-installation" title="永久链接至标题">¶</a></h2>
<p>This part includes more details of installation (e.g. building a conda environment) and guidance for the GPU version of TensorFlow environment installation.</p>
<div class="section" id="environment-configuration-before-installation">
<h3>Environment configuration before installation<a class="headerlink" href="#environment-configuration-before-installation" title="永久链接至标题">¶</a></h3>
<p>Before installing TensorFlow, we need to set up a proper environment with the following steps:</p>
<ol class="arabic simple">
<li>Check if your computer has an NVIDIA graphics card and install the GPU version of TensorFlow in order to take advantages of its powerful capability of computation acceleration <a class="footnote-reference" href="#f1" id="id1">[1]</a> , or, just install CPU version if not so. To be more specific, the CUDA Computing Capability of your graphics card that you can check on <a class="reference external" href="https://developer.nvidia.com/cuda-gpus/">NVIDIA official website</a> should not be less than 3.0.</li>
<li>Install the Python environment. Anaconda is recommended. It is an open-source release version of Python that provides a full environment for scientific computation including common libraries such as NumPy and SciPy, or you can choose your favorite ones of course. Note that TensorFlow only supports Python Ver 3.X under Windows when we write this handbook.<ul>
<li>You can choose to add the directory of Anaconda into the PATH (though not recommended by the installation wizard). It enables you to call all Anaconda commands under command line or Powershell directly. You can always call them under the Anaconda Prompt started in the Start Menu.</li>
</ul>
</li>
<li>(For GPU version installation) Install the NVIDIA graphics driver, <a class="reference external" href="https://developer.nvidia.com/cuda-downloads">CUDA Toolkit</a> and <a class="reference external" href="https://developer.nvidia.com/cudnn">cuDNN</a>. You should note that:<ul>
<li>We recommend you install it through the following order: 1) latest NVIDIA graphics driver 2) CUDA (without selecting the built-in driver when installing since the built-in ones may be out-of-date) 3) cuDNN;</li>
<li>There is a quite simple way to install drivers in Ubuntu. First click “Software &amp; Updates” in “System Setting”, then toggle on “Using NVIDIA binary driver” option in “Additional Drivers” and click “Apply Changes” for system to install NVIDIA drivers automatically, otherwise, it won’t be peaceful for NVIDIA installation on Linux. You should disable the built-in graphics driver Nouveau and Secure Boot function of the motherboard. You can seek a more detailed guidance <a class="reference external" href="https://www.linkedin.com/pulse/installing-nvidia-cuda-80-ubuntu-1604-linux-gpu-new-victor/">here</a>;</li>
<li>The version of CUDA Toolkit and cuDNN must agree with the requirements on TensorFlow official website which does not always require the latest version.</li>
<li>You have to copy the downloaded files of cuDNN to the installation directory of CUDA to complete cuDNN installation.</li>
</ul>
</li>
</ol>
</div>
<div class="section" id="install">
<h3>Install<a class="headerlink" href="#install" title="永久链接至标题">¶</a></h3>
<p>These are the following steps of TensorFlow installation under Anaconda (taking Windows as example):</p>
<ol class="arabic simple">
<li>Create a conda environment named <code class="docutils literal"><span class="pre">tensorflow</span></code></li>
</ol>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">conda</span> <span class="n">create</span> <span class="o">-</span><span class="n">n</span> <span class="n">tensorflow</span> <span class="n">python</span><span class="o">=</span><span class="n">X</span><span class="o">.</span><span class="n">X</span> <span class="c1"># Substitute &quot;X.X&quot; with your own Python version, e.g. &quot;3.6&quot;.</span>
</pre></div>
</div>
<ol class="arabic simple" start="2">
<li>Activate the environment</li>
</ol>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">activate</span> <span class="n">tensorflow</span>
</pre></div>
</div>
<ol class="arabic simple" start="3">
<li>Use pip to install TensorFlow</li>
</ol>
<p>Install the CPU version</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="n">tensorflow</span>
</pre></div>
</div>
<p>Or, install the GPU version</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="n">tensorflow</span><span class="o">-</span><span class="n">gpu</span>
</pre></div>
</div>
<p>You can also choose to install the Nightly version of TensorFlow if you want. This version may include some latest features compared to the official version (e.g. the Eager Execution mode in this handbook was only supported in the Nightly version before TensorFlow Ver 1.8) yet with some instability. You can do so by running <code class="docutils literal"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">tf-nightly</span></code> (CPU version) or <code class="docutils literal"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">tf-nightly-gpu</span></code> (GPU version) in a new virtual environment. If you are going to install the GPU version, it may require higher versions of CUDA and cuDNN. Fortunately different versions of CUDA and cuDNN can coexist.</p>
<p>If it is slow to install through pip command in China, you may want to try <a class="reference external" href="https://mirrors.tuna.tsinghua.edu.cn/help/tensorflow/">TensorFlow mirror on TUNA</a>.</p>
</div>
</div>
<div class="section" id="first-program">
<h2>First Program<a class="headerlink" href="#first-program" title="永久链接至标题">¶</a></h2>
<p>We write a piece of code to verify the installation.</p>
<p>Enter <code class="docutils literal"><span class="pre">activate</span> <span class="pre">tensorflow</span></code> under command line to enter the previously built conda environment with TensorFlow. Then enter <code class="docutils literal"><span class="pre">python</span></code> to enter Python environment. Input the following codes line by line:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tensorflow</span> <span class="kn">as</span> <span class="nn">tf</span>
<span class="n">tf</span><span class="o">.</span><span class="n">enable_eager_execution</span><span class="p">()</span>

<span class="n">A</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">constant</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]])</span>
<span class="n">B</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">constant</span><span class="p">([[</span><span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">],</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">]])</span>
<span class="n">C</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">)</span>

<span class="k">print</span><span class="p">(</span><span class="n">C</span><span class="p">)</span>
</pre></div>
</div>
<p>If the output is:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">tf</span><span class="o">.</span><span class="n">Tensor</span><span class="p">(</span>
<span class="p">[[</span><span class="mi">19</span> <span class="mi">22</span><span class="p">]</span>
<span class="p">[</span><span class="mi">43</span> <span class="mi">50</span><span class="p">]],</span> <span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">int32</span><span class="p">)</span>
</pre></div>
</div>
<p>We can draw conclusions that TensorFlow was successfully installed. It’s normal for the program to output some prompt messages when running.</p>
<p>Here we use Python. You can get Python tutorials on <a class="reference external" href="https://docs.python.org/3/tutorial/">https://docs.python.org/3/tutorial/</a>. From now on we assume that readers are familiar with the basics of Python. Relax, Python is easy to handle and advanced features of Python will be barely involved in TensorFlow. We recommend you to use <a class="reference external" href="http://www.jetbrains.com/pycharm/">PyCharm</a> as your Python IDE. If you are a student with an email address ended with .edu, you can apply for a free license <a class="reference external" href="http://www.jetbrains.com/student/">here</a>. You can always download PyCharm Community version whose main functions do not differ that much from the former if you do not meet the aforementioned criteria.</p>
<table class="docutils footnote" frame="void" id="f1" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>The effect of acceleration is relative to the GPU performance. It won’t be satisfactory if you have a high performance CPU and a beginner level GPU where the acceleration rate will be like 1-2. However, the acceleration rate may reach 10 or even higher under specific models if you have a powerful GPU (e.g. NVIDIA GeForce GTX 1080 Ti or NVIDIA GeForce TITAN Series are powerful graphics card types when this handbook was being written). Meanwhile, the acceleration rate is also influenced by the running task itself. The beginner level models of TensorFlow do not require too much performance as the CPU version is adequate. You may determine if you will purchase a higher level graphics card to get faster training speed after you master the basics of TensorFlow.</td></tr>
</tbody>
</table>
</div>
<div class="section" id="upgrade-to-a-new-version">
<h2>Upgrade to A New Version<a class="headerlink" href="#upgrade-to-a-new-version" title="永久链接至标题">¶</a></h2>
<p>TensorFlow is updated frequently. If you want to upgrade TensorFlow, please enter the conda environment with TensorFlow installed and type the following command</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="n">tensorflow</span> <span class="o">--</span><span class="n">upgrade</span>
</pre></div>
</div>
<p>If you want to install a specific version of TensorFlow, please type</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="n">tensorflow</span><span class="o">==</span><span class="mf">1.8</span><span class="o">.</span><span class="mi">0</span>      <span class="c1"># here 1.8.0 is the specified version</span>
</pre></div>
</div>
<p>Upgrade of TensorFlow can be risky, and there may be errors whe you import TensorFlow after the upgrade. The simpler way is to delete the current conda environment and reinstall it. The following conda commands may be useful:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">conda</span> <span class="nb">list</span>                                             <span class="c1"># List all packages and versions in the current conda environment</span>
<span class="n">conda</span> <span class="n">env</span> <span class="nb">list</span>                                         <span class="c1"># List all conda environments</span>
<span class="n">conda</span> <span class="n">create</span> <span class="o">--</span><span class="n">name</span> <span class="n">new_env_name</span> <span class="o">--</span><span class="n">clone</span> <span class="n">old_env_name</span>  <span class="c1"># Backup the current conda environment `old_env_name` to `new_env_name`</span>
<span class="n">conda</span> <span class="n">env</span> <span class="n">remove</span> <span class="o">-</span><span class="n">n</span> <span class="n">tensorflow</span>                         <span class="c1"># Delete the conda environment named `tensorflow`</span>
</pre></div>
</div>
</div>
</div>


           </div>
           <div class="articleComments">
            
           </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="basic.html" class="btn btn-neutral float-right" title="TensorFlow Basic" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="preface.html" class="btn btn-neutral" title="Preface" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Xihan Li（雪麒）.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../',
            VERSION:'0.3 beta',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../_static/jquery.js"></script>
      <script type="text/javascript" src="../_static/underscore.js"></script>
      <script type="text/javascript" src="../_static/doctools.js"></script>
      <script type="text/javascript" src="../_static/translations.js"></script>

  

  
  
    <script type="text/javascript" src="../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>